#!usr/bin/env python 
# -*- coding: utf-8 -*-
import datetime
import os
import platform

import re

from module import PageInfo
from script import Saver

__author__ = '锄禾'

class Device:
    def __init__(self, plan, device_id):
        Saver.save_crawler_log(plan.logPath, "Step : Init device : " + device_id)
        self.id = device_id
        Saver.save_crawler_log(plan.logPath, "id：" + self.id)
        self.statue = self.get_device_statue()
        Saver.save_crawler_log(plan.logPath, "statue：" + self.statue)
        self.logPath = self.create_device_folder(plan)
        self.name = self.get_device_name()
        self.model = self.get_device_model()
        self.version = self.get_device_sys_version()
        self.accountInfo = []
        self.screenResolution = self.get_screen_resolution()
        self.screenshotPath = self.create_screenshot_folder()
        self.beginCrawlTime = datetime.datetime.now()
        self.endCrawlTime = datetime.datetime.now()
        self.unCrawledNodes = []
        self.hasCrawledNodes = []
        self.hasCrawledPage = []
        self.hasCrawledActivities = []
        self.saveScreenNum = 0
        self.jump_out_time = 0
        self.crawlStatue = "Uninit"
        self.failedTime = 0
        self.page_now = PageInfo.Page()

    def create_screenshot_folder(self):
        path = self.logPath + "/Screenshot"
        Saver.save_crawler_log(self.logPath, "Step：create screenshot folder")
        if not os.path.exists(path):
            os.makedirs(path)
        return path


    def get_screen_resolution(self):
        Saver.save_crawler_log(self.logPath, "Step：get screen resolution")
        command = 'adb -s ' +self.id + ' shell dumpsys window'
        resolution = []
        result = os.popen(command).readlines()
        x = ''
        y = ''
        for line in result:
            if 'init=' in line:
                r = re.findall(r'\d+', line)
                x = r[0]
                y = r[1]
                del x
        resolution.append(x)
        resolution.append(y)
        Saver.save_crawler_log(self.logPath, resolution)
        del command, result, x, y
        return resolution

    def get_device_sys_version(self):
        command = 'adb -s ' + self.id + ' shell getprop ro.build.version.release'
        result = os.popen(command).read()
        Saver.save_crawler_log(self.logPath, "sys version：" + result)
        del command
        return result


    def get_device_model(self):
        command = 'adb -s ' + self.id + ' shell getprop ro.product.model'
        model = os.popen(command).read()
        Saver.save_crawler_log(self.logPath, "device model：" + model)
        del command
        return model

    def get_device_statue(self):
        if platform.system() != "Windows":
            check_lock_command = "adb -s " + self.id + " shell dumpsys window policy | grep mShowingLockscreen"
            check_keyguard_command = "adb -s " + self.id + " shell dumpsys window policy | grep isStatusBarKeyguard"
        else:
            check_lock_command = "adb -s " + self.id + " shell dumpsys window policy | findstr mShowingLockscreen"
            check_keyguard_command = "adb -s " + self.id + " shell dumpsys window policy | findstr isStatusBarKeyguard"
        check_lock_statue = os.popen(check_lock_command).read()
        check_keyguard_statue = os.popen(check_keyguard_command).read()
        if check_lock_statue == "" and check_keyguard_statue == "":
            return "unConnect/powerOff"
        else:
            str1 = 'mShowingLockscreen='
            str2 = 'mShowingDream='
            str3 = 'isStatusBarKeyguard='
            str4 = 'mNavigationBar='
            index1 = check_lock_statue.find(str1)
            index2 = check_lock_statue.find(str2)
            index3 = check_keyguard_statue.find(str3)
            index4 = check_keyguard_statue.find(str4)
            check_lock_statue = check_lock_statue[index1 + len(str1):index2 - 1]
            check_keyguard_statue = check_keyguard_statue[index3 + len(str3):index4 - 4]
            if check_lock_statue != 'true' and check_keyguard_statue != 'true':
                del check_lock_command, check_keyguard_statue, check_lock_statue, check_keyguard_command, str1, str2, str3, str4, index1, index2, index3, index4
                return "unlock"
            else:
                del check_lock_command, check_keyguard_statue, check_lock_statue, check_keyguard_command, str1, str2, str3, str4, index1, index2, index3, index4
                return "screenlocked"


    def create_device_folder(self, plan):
        deviceid = self.id
        if '.' in deviceid:
            deviceid = deviceid.replace('.', '_')
        if ':' in deviceid:
            deviceid = deviceid.replace(':', '_')
        path = plan.logPath + '/' + deviceid
        if not os.path.exists(path):
            os.makedirs(path)
        del plan, deviceid
        return path

    def get_device_name(self):
        command = 'adb -s ' + self.id + ' shell getprop ro.product.name'
        device_name = os.popen(command).read()
        Saver.save_crawler_log(self.logPath, 'device name：' + device_name)
        del command
        return device_name